﻿
Namespace PL.CRMSDK
    Public Class PurchaseOrderDetail_PL
        Inherits PLBase
        Implements IPLProcess


        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
        End Sub

        Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating

        End Sub

        Public Overrides Function Delete() As Boolean
            'Throw New NotImplementedException("Delete To Come...")
            Return False
        End Function

        Public Overrides Function Validate() As Boolean
            'Throw New NotImplementedException("Validate To Come...")
            Return True
        End Function

        Public Overrides Sub ValidateRow(row As DataRow)
            row.ClearErrors()
            'Mandatory Fields
            'TODO: Cache MetaString in AppManager, so here we read it once maybe for nothing
            'Dim ms As MetaString
            'ms = MetaString.GetMetaStringByCode("Mandatory", AppManager.ApplicationOption.LanguageId)
            If IsDBNull(row("PurchaseOrderID_FK")) Then
                row.SetColumnError("PurchaseOrderID_FK", "Mandatory")
            End If
            If IsDBNull(row("LineNumber")) Then ' And Not InvBatchNoSEQ Then
                row.SetColumnError("LineNumber", "Mandatory")
            End If
            If IsDBNull(row("ProductID_FK")) Then
                row.SetColumnError("ProductID_FK", "Mandatory")
            End If
            If IsDBNull(row("ReceivedQty")) Then
                row.SetColumnError("ReceivedQty", "Mandatory")
            End If
            If IsDBNull(row("CurrencyID_FK")) Then
                row.SetColumnError("CurrencyID_FK", "Mandatory")
            End If
            If IsDBNull(row("StatusID_FK")) Then
                row.SetColumnError("StatusID_FK", "Mandatory")
            End If
            '
            If row.HasErrors Then
                row.RowError = "Please check your columns, a validation error occur"
            End If
        End Sub

        Public Function BatchRecords(dataRow() As DataRow) As Boolean Implements IPLProcess.BatchRecords
            Return True
        End Function

        Public Function CurrentRecord(dataRow As DataRow) As Boolean Implements IPLProcess.CurrentRecord
            Return True
        End Function

        Public Function NewRecord(dataRow As DataRow) As Boolean Implements IPLProcess.NewRecord
            ' Set all Default values of new record
            ' currency, Qty's,Status etc of a new PO Line
            Try
                If Me.ID IsNot Nothing Then
                    dataRow("PurchaseOrderDetailId") = ID
                Else
                    dataRow("PurchaseOrderDetailId") = Guid.NewGuid
                End If
                'Line number is the max + 1 of line numbers
                'do a LINQ command to get the MAX of the Line details then to a variable + 1
                Dim aLineNo As Integer = 0
                If dataRow.Table.DataSet.Tables("PurchaseOrderDetails").Rows.Count > 0 Then
                    aLineNo = (From a In dataRow.Table.DataSet.Tables("PurchaseOrderDetails").AsEnumerable() Select a.Field(Of Integer)("LineNumber")).Max
                End If
                dataRow("LineNumber") = aLineNo + 1


                'RequestedUnitId by dafault will read 
                'Product Purchase unit or Itemmaster Spec Purchase unit or Productvendor unit or
                ' dataRow("RequestedUnitID_FK") 

                ' this will be copyied from the released Lot transaction  once recieved
                ' dataRow("ReceivedUnitID_FK")

                'Session Society Currency
                'dataRow("CurrencyID_FK")

                'Not recieved by default
                ' dataRow("StatusID_FK")

                dataRow("ExpectedDate") = Now()
                'Will be copied by Release Lot Tranasction
                'dataRow("ReceivedDate") 

                dataRow("Discount") = 0
                dataRow("Payable") = True
                dataRow("Quantity") = 1
                dataRow("ReceivedQty") = 0
                dataRow("UnitPrice") = -1

                'dataRow("RefCS")
                Return True

            Catch ex As Exception
                Throw ex

                Return False
            End Try




        End Function
    End Class
End Namespace
